package com.example.animalcase;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignupActivity extends AppCompatActivity {

    private EditText mSignupEmail;
    private EditText mSignupPassword;
    private EditText mSignupConfirm;
    private Button mSignupButton;
    private TextView mLoginRedirectText;

    DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        mSignupEmail=(EditText)findViewById(R.id.signup_email);
        mSignupPassword=(EditText)findViewById(R.id.signup_password);
        mSignupConfirm=(EditText)findViewById(R.id.signup_confirm);
        mSignupButton=(Button) findViewById(R.id.signup_button);
        mLoginRedirectText=(TextView) findViewById(R.id.loginRedirectText);

        databaseHelper=new DatabaseHelper(this);

        mSignupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email=mSignupEmail.getText().toString();
                String password=mSignupPassword.getText().toString();
                String confirmPassword=mSignupConfirm.getText().toString();

                if (email.equals("")||password.equals("")||confirmPassword.equals("")){
                    Toast.makeText(SignupActivity.this,"All fields are mandatory",Toast.LENGTH_SHORT).show();
                }else {
                    if (password.equals(confirmPassword)){
                        Boolean checkUserEmail=databaseHelper.checkEmail(email);
                        if (checkUserEmail == false){
                            Boolean insert =databaseHelper.insertData(email,password);
                            if (insert==true){
                                Toast.makeText(SignupActivity.this,"Signup Successfully",Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
                                startActivity(intent);
                            }else {
                                Toast.makeText(SignupActivity.this,"Signup Failed",Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(SignupActivity.this,"User already exists,Please login",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(SignupActivity.this,"Invalid PAssword",Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

        mLoginRedirectText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getApplicationContext(),LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}
